package kevin.framework.basic.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import kevin.framework.basic.common.ui.AjaxResult;
import kevin.framework.basic.domain.Dict;
import kevin.framework.basic.service.IBaseService;
import kevin.framework.basic.service.IDictService;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Controller
@RequestMapping(value = "/dict")
public class DictController extends ResetfulBaseController<Dict, String> {

    IDictService dictService;

    @Resource(name = "dictService")
    @Override
    public void setService(IBaseService<Dict, String> service) {
        this.service = service;
        dictService = (IDictService)service;
    }

    @RequestMapping(value = "/editor", method = RequestMethod.POST)
    public @ResponseBody AjaxResult editor(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        String ext = request.getParameter("ext");
        String desp = URLDecoder.decode(request.getParameter("desp"), "utf-8");
        Dict d = new Dict();
        d.setDesp(desp);
        d.setDkey("test");
        d.setDvalue("test");
        d.setTypeid("test");
        d.setExtFiled(ext);
        if (StringUtils.isEmpty(id)) {
            dictService.insert(d);
        } else {
            d.setId(id);
            dictService.update(d);
        }
        return this.returnSuccess();
    }

    @Override
    public String saveFilter(Dict bean, boolean isupdate, HttpServletRequest request) {
        if ("-111111".equals(bean.getTypeid())) {
            return "不能添加typeid为-111111的数据";
        }
        return super.saveFilter(bean, isupdate, request);
    }

    /*
     * 页面 views/base/entityPath 文件夹路径
     * ***/
    @Override
    public String setPageModelPath(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return "base" + File.separator + entityPath;
    }

    @Override
    protected Map<String, Object> setListParams(HttpServletRequest request) throws Exception {

        Map<String, Object> prMap = new HashMap<String, Object>();
        prMap.put("typeidexc", "-111111");
        return prMap;
    }

    @Override
    public boolean saveFiles(MultipartHttpServletRequest request, Map<String, MultipartFile> fileMap,
        Map<String, String> uploadResult) {

        MultipartFile file = fileMap.get("pdffile");
        if (file != null) {
            String userName = request.getParameter("username");
            String userPwd = request.getParameter("userpwd");
            System.out.println("收到pdf文件 " + userName + " " + userPwd + " " + file.getOriginalFilename());
            String destPath = this.getContextDirPath(request) + file.getOriginalFilename();
            File dest = new File(destPath);
            try {
                file.transferTo(dest);
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return true;
    }
}
